exports.getToken = function (req, res) {

    //获取参数信息
    var bodyParams = req.routeInfo.bodyParams;

    //返回结果
    var resultData = {};

    //获取随机token
    var uts_token = common.getVerificationLetter()

    //是否存在用户
    var ifTrue =false

    //存储命名空间id
    var n_id = ""

    //任务流创建
    async.waterfall([

        //查询命名空间
        //判断是否存在
        //添加或更新用户表  socket_user
        //添加或更新token  socket_user_token
        (cb)=> {

            //查询是否已经存在
            db.field('n_id').where({
                "n_appid" : bodyParams.severAppId
            }).select("socket_namespace",(rs) => {
                //不存在
                if(rs.length == 0){
                    return RES.errorResponse(res,"此appId不存在");
                }
                n_id = rs[0].n_id
                cb();
            })

        },

        (cb)=> {

            //查询是否已经存在

            db.where({
                "u_username" : bodyParams.accountNumber,
                "u_nid"       : n_id,
                "u_is_delete" : 0
            }).select("socket_user",(rs) => {
                if(rs.length != 0){
                    ifTrue = true
                }
                cb();
            })

        },

        (cb) => {

            if(ifTrue){
                //更新
                db.where({
                    "u_username" : bodyParams.accountNumber,
                    "u_is_delete" : 0
                }).update({
                    "u_nickname":bodyParams.name,
                    "u_header":bodyParams.portraitUri,
                    "u_create_time":common.nowTime(),
                },"socket_user",(rs)=>{
                    resultData.accountNumber = bodyParams.accountNumber
                    //更新
                    db.where({
                        "ut_uid" : bodyParams.accountNumber,
                        "ut_is_delete" : 0
                    }).update({
                        "ut_token":uts_token,//用户token
                        "ut_create_time":common.nowTime()//创建时间
                    },"socket_user_token",(rs)=>{
                        resultData.token = uts_token
                        cb()
                    })
                })
            }else{

                //添加
                db.add({
                    "u_username":bodyParams.accountNumber,
                    "u_nickname":bodyParams.name,
                    "u_header"  :bodyParams.portraitUri,
                    "u_nid"      :n_id,
                    "u_create_time":common.nowTime(),
                },"socket_user",(rs)=>{
                    resultData.accountNumber = bodyParams.accountNumber
                    //添加
                    db.add({
                        "ut_uid":bodyParams.accountNumber,
                        "ut_token":uts_token,
                        "ut_create_time":common.nowTime(),
                    },"socket_user_token",(rs)=>{
                        resultData.token = uts_token
                        cb()
                    })
                })
            }

        },
    ],()=> {

        //返回最终拼接结果
        RES.successResponse(res,resultData);

    });
    
}